﻿using System;
using System.Runtime.InteropServices;

namespace WindowsAPI.COM
{
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("00BB2762-6A77-11D0-A535-00C04FD7D062")]
    public interface IAutoComplete
    {
        /// <summary>Initializes the autocomplete object</summary>
        /// <param name="hwndEdit">Handle to the window for the system edit control that is to have autocompletion enabled</param>
        /// <param name="punkACL">
        /// Pointer to the IUnknown interface of the string list object that is responsible for generating candidates 
        /// for the completed string. The object must expose an IEnumString interface 
        /// </param>
        /// <param name="pwszRegKeyPath">
        /// Pointer to an optional null-terminated Unicode string that gives the registry path, 
        /// including the value name, where the format string is stored as a REG_SZ value. 
        /// The autocomplete object first looks for the path under HKEY_CURRENT_USER. 
        /// If it fails, it then tries HKEY_LOCAL_MACHINE. 
        /// For a discussion of the format string, see the definition of pwszQuickComplete 
        /// </param>
        /// <param name="pwszQuickComplete">
        /// Pointer to an optional string that specifies the format to be used if the user enters some text 
        /// and presses CTRL+ENTER. Set this parameter to NULL to disable quick completion. Otherwise, 
        /// the autocomplete object treats pwszQuickComplete as a sprintf format string, and the text 
        /// in the edit box as its associated argument, to produce a new string. For example, set pwszQuickComplete 
        /// to "http://www. %s.com/". When a user enters "MyURL" into the edit box and presses CTRL+ENTER, the text 
        /// in the edit box is updated to "http://www.MyURL.com/" 
        /// </param>
        /// <returns></returns>
        [PreserveSig]
        int Init(
            IntPtr hwndEdit,
            [MarshalAs(UnmanagedType.IUnknown)] object punkACL,
            [MarshalAs(UnmanagedType.LPWStr)] string pwszRegKeyPath,
            [MarshalAs(UnmanagedType.LPWStr)] string pwszQuickComplete);

        /// <summary>Enables or disables autocompletion</summary>
        /// <param name="fEnable">Value that is set to TRUE to enable autocompletion, or to FALSE to disable it.</param>
        /// <returns></returns>
        [PreserveSig]
        int Enable(int fEnable);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("EAC04BC0-3791-11D2-BB95-0060977B464C")]
    public interface IAutoComplete2
    {
        /// <summary>Initializes the autocomplete object</summary>
        /// <param name="hwndEdit">Handle to the window for the system edit control that is to have autocompletion enabled.</param>
        /// <param name="punkACL">
        /// Pointer to the IUnknown interface of the string list object that is responsible for generating candidates 
        /// for the completed string. The object must expose an IEnumString interface.
        /// </param>
        /// <param name="pwszRegKeyPath">
        /// Pointer to an optional null-terminated Unicode string that gives
        /// the registry path, including the value name, where the format 
        /// string is stored as a REG_SZ value. The autocomplete object 
        /// first looks for the path under HKEY_CURRENT_USER. If it fails,
        /// it then tries HKEY_LOCAL_MACHINE. For a discussion of the 
        /// format string, see the definition of pwszQuickComplete.
        /// </param>
        /// <param name="pwszQuickComplete">
        /// Pointer to an optional string that specifies the format to be
        /// used if the user enters some text and presses CTRL+ENTER. Set
        /// this parameter to NULL to disable quick completion. Otherwise,
        /// the autocomplete object treats pwszQuickComplete as a sprintf 
        /// format string, and the text in the edit box as its associated 
        /// argument, to produce a new string. For example, set 
        /// pwszQuickComplete to "http://www. %s.com/". When a user enters
        /// "MyURL" into the edit box and presses CTRL+ENTER, the text in 
        /// the edit box is updated to "http://www.MyURL.com/".
        /// </param>
        /// <returns></returns>
        [PreserveSig]
        int Init(
            IntPtr hwndEdit,
            [MarshalAs(UnmanagedType.IUnknown)] object punkACL,
            [MarshalAs(UnmanagedType.LPWStr)] string pwszRegKeyPath,
            [MarshalAs(UnmanagedType.LPWStr)] string pwszQuickComplete
        );

        /// <summary>Enables or disables autocompletion</summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [PreserveSig]
        int Enable(bool value);

        /// <summary>Sets the current autocomplete options</summary>
        /// <param name="dwFlag"></param>
        /// <returns></returns>
        [PreserveSig]
        int SetOptions(AUTOCOMPLETEOPTIONS dwFlag);

        /// <summary>Retrieves the current autocomplete options</summary>
        /// <param name="pdwFlag"></param>
        /// <returns></returns>
        [PreserveSig]
        int GetOptions(out AUTOCOMPLETEOPTIONS pdwFlag);
    }

    /// <summary>
    /// Specifies values used by IAutoComplete2::GetOptions and "IAutoComplete2.SetOptions" for options surrounding autocomplete.
    /// </summary>
    /// <remarks>
    /// [AUTOCOMPLETEOPTIONS Enumerated Type]
    /// http://msdn.microsoft.com/en-us/library/bb762479.aspx
    /// </remarks>
    [Flags]
    public enum AUTOCOMPLETEOPTIONS
    {
        /// <summary>Do not autocomplete.</summary>
        ACO_NONE = 0x0000,

        /// <summary>Enable the autosuggest drop-down list.</summary>
        ACO_AUTOSUGGEST = 0x0001,

        /// <summary>Enable autoappend.</summary>
        ACO_AUTOAPPEND = 0x0002,

        /// <summary>
        /// Add a search item to the list of 
        /// completed strings. When the user selects 
        /// this item, it launches a search engine.
        /// </summary>
        ACO_SEARCH = 0x0004,

        /// <summary>Do not match common prefixes, such as "www." or "http://".</summary>
        ACO_FILTERPREFIXES = 0x0008,

        /// <summary>Use the TAB key to select an item from the drop-down list.</summary>
        ACO_USETAB = 0x0010,

        /// <summary>Use the UP ARROW and DOWN ARROW keys to display the autosuggest drop-down list.</summary>
        ACO_UPDOWNKEYDROPSLIST = 0x0020,

        /// <summary>
        /// Normal windows display text left-to-right 
        /// (LTR). Windows can be mirrored to display languages 
        /// such as Hebrew or Arabic that read right-to-left (RTL). 
        /// Typically, control text is displayed in the same 
        /// direction as the text in its parent window. If 
        /// ACO_RTLREADING is set, the text reads in the opposite 
        /// direction from the text in the parent window.
        /// </summary>
        ACO_RTLREADING = 0x0040,

        /// <summary>
        /// [Windows Vista and later]. If set, the 
        /// autocompleted suggestion is treated as a phrase 
        /// for search purposes. The suggestion, Microsoft 
        /// Office, would be treated as "Microsoft Office" 
        /// (where both Microsoft AND Office must appear in 
        /// the search results).
        /// </summary>
        ACO_WORD_FILTER = 0x0080,

        /// <summary>
        /// [Windows Vista and later]. Disable prefix 
        /// filtering when displaying the autosuggest dropdown. 
        /// Always display all suggestions.
        /// </summary>
        ACO_NOPREFIXFILTERING = 0x0100
    }
}
